define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'production_process/index' + location.search,
                    add_url: 'production_process/add',
                    // edit_url: 'production_process/edit',
                    // del_url: 'production_process/del',
                    // multi_url: 'production_process/multi',
                    table: 'production_process',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id',class:"sid", title: __('Id')},
                        {field: 'title', title: __('Title')},
                        {field: 'number', title: __('Number')},
                        {field: 'image', title: __('Image'), events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'goods_num', title: __('Goods_num')},
                        {field: 'consume_num', title: __('Consume_num')},
                        {field: 'cost', title: __('Cost'), operate:'BETWEEN'},
                        {field: 'operator', title: __('Operator')},
                        {field: 'production_number', title: __('Production_number')},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });
            $('table').on('dblclick','td',function(){
                // if($(this).parents('tr').find('.btn-editone').length >0){
                //     return  false;
                // }
                var sid = $(this).parents('tr').find('.sid').html()
                var url = Config.this_url+'/production_process/detail?ids='+sid

                layer.open({
                    type: 2, // 1-在content中写html代码， 2-在content中写src路径
                    area: ['100%', '100%'],
                    title: "生产单详情",
                    content: url,
                    // maxmin:true
                    // content: Config.this_url+'/purchase/requisition',
                })
            });
            // 修改添加窗口的大小
            $(".btn-add").data("area", ["100%", "100%"]);
            // 修改编辑窗口的大小
            $(".btn-edit").data("area", ["50%", "60%"]);
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            $(".btn-add").click(function () {
                if($('#c-storehouse_id').val()== 0 ){
                    layer.msg('未选中出库仓库')
                    return false;
                }
                layer.open({
                    type: 2, // 1-在content中写html代码， 2-在content中写src路径
                    area: ['70%', '90%'],
                    title: "选择商品",
                    content: 'seach',
                })
            })
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        detail:function(){
            $('.cancelbill').click(function () {
                var url =  'production_process/cancelbill'+location.search
                layer.prompt({title: '输入红冲原因', formType: 2}, function(pass, index){
                    if(pass == ''){
                        layer.msg('不能为空');
                        return false;
                    }
                    $.post(url,{reason:pass},function (res) {
                        layer.msg(res.msg)
                        if(res.code == 1){
                            layer.close(index);
                            setTimeout(function () {
                                location.reload();
                            }, 800);
                            window.parent.$(".toolbar .btn-refresh").trigger('click');
                        }
                    })
                });
            })


            Controller.api.bindevent();
        },

        seach:function(){
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'goods/goods/index'+ location.search,
                    table: '',
                }
            });

            var table = $("#table");
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'weigh',
                pageSize:15,
                // searchFormVisible: true,
                searchFormTemplate: 'customformtpl',
                columns: [
                    [
                        // {checkbox: true,operate:false},
                        {field: 'id', title: 'ID', operate: false,class:'sid'},
                        {field: 'number', title: __('number'),sortable:true},
                        {field: 'title', title: __('title'),  operate: false,sortable:true},
                        {field: 'specs', title: __('specs')},
                        {field: 'stock', title: __('stock'),sortable:true},
                        {field: 'createtime', title: __('Create time'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            buttons: [
                                {
                                    name: 'returnorder',
                                    title: __('选中'),
                                    classname: 'btn btn-xs btn-success btn-click editstatus',
                                    icon: 'fa fa-fighter-jet',
                                    hidden:function(row){
                                        // if(row.status != '审核'){
                                        //     return true;
                                        // }
                                    },
                                    click: function(data){
                                        var sid =$(this).parents('tr').find('.sid').html()
                                        $(this).attr('style','display:none')
                                        parent.getrequisition(sid);
                                        // console.log(parent.getrequisition(sid))
                                        // console.log('返回结果:'+res)
                                    }}
                            ],
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });
            $('table').on('dblclick','td',function(){
                var sid =$(this).parents('tr').find('.sid').html()
                parent.getrequisition(sid);
                var index = parent.Layer.getFrameIndex(window.name);
                parent.Layer.close(index);
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});